<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/9/4
 * Time: 19:16
 */

namespace app\home\controller;
use app\home\model\WechatUser;
use think\Db;

/**
 * Class Paper
 * @package app\home\controller
 * 每日统计
 */
class Paper extends Base
{
    /**
     * 首页
     */
    public function index(){
        $userId = session('userId');
        $role = WechatUser::where('openid',$userId)->value('role');
        if ($role == 0 && $userId != "oJuxj0_Ju_RIb2HbwYxXwAdYLch0"){
            // 学生 或者 非 指定人员 暂无权限
            $this->redirect('Journal/null');
        }
        // 当前时间
        $this->assign('time',date('Y-m-d'));
        return $this->fetch();
    }
    /**
     * 时间筛选
     */
    public function select(){
        $time = strtotime(input('time')); // 格式 2018-08-09
        $now = strtotime(date('Y-m-d'));
        if ($time > $now || ($time == $now  && date('H') <= 9 && date('i') < 10)){
            // 暂无数据
            return ['code' => 0,'msg' => '请在当日09:10后查看相应数据统计！'];
        }
        $info = Db::name('paper')->where(['create_time' => $time])->find();
        if ($info){
            $time = $time - 24*60*60;  // 前一天数据
            $infos = Db::name('paper')->where(['create_time' => $time])->find();
            if ($infos){
                // 有数据
                if ($info['sub_sum'] > $infos['sub_sum']){
                    $info['sub_sum_flag'] = "<span style='color: green'>↑</span>";
                }elseif ($info['sub_sum'] == $infos['sub_sum']){
                    $info['sub_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                }else{
                    $info['sub_sum_flag'] = "<span style='color: red'>↓</span>";
                }

                if ($info['reg_sum'] > $infos['reg_sum']){
                    $info['reg_sum_flag'] = "<span style='color: green'>↑</span>";
                }elseif ($info['reg_sum'] == $infos['reg_sum']){
                    $info['reg_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                }else{
                    $info['reg_sum_flag'] = "<span style='color: red'>↓</span>";
                }

                if ($info['reg_stu'] > $infos['reg_stu']){
                    $info['reg_stu_flag'] = "<span style='color: green'>↑</span>";
                }elseif ($info['reg_stu'] == $infos['reg_stu']){
                    $info['reg_stu_flag'] = "<span style='color: #F9CB79'>-</span>";
                }else{
                    $info['reg_stu_flag'] = "<span style='color: red'>↓</span>";
                }

                if ($info['reg_tea'] > $infos['reg_tea']){
                    $info['reg_tea_flag'] = "<span style='color: green'>↑</span>";
                }elseif ($info['reg_tea'] == $infos['reg_tea']){
                    $info['reg_tea_flag'] = "<span style='color: #F9CB79'>-</span>";
                }else{
                    $info['reg_tea_flag'] = "<span style='color: red'>↓</span>";
                }

                if ($info['sign_sum'] > $infos['sign_sum']){
                    $info['sign_sum_flag'] = "<span style='color: green'>↑</span>";
                }elseif ($info['sign_sum'] == $infos['sign_sum']){
                    $info['sign_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                }else{
                    $info['sign_sum_flag'] = "<span style='color: red'>↓</span>";
                }

                if ($info['journal_sum'] > $infos['journal_sum']){
                    $info['journal_sum_flag'] = "<span style='color: green'>↑</span>";
                }elseif ($info['journal_sum'] == $infos['journal_sum']){
                    $info['journal_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                }else{
                    $info['journal_sum_flag'] = "<span style='color: red'>↓</span>";
                }

                if ($info['homework_sum'] > $infos['homework_sum']){
                    $info['homework_sum_flag'] = "<span style='color: green'>↑</span>";
                }elseif ($info['homework_sum'] == $infos['homework_sum']){
                    $info['homework_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                }else{
                    $info['homework_sum_flag'] = "<span style='color: red'>↓</span>";
                }

                if ($info['play_sum'] > $infos['play_sum']){
                    $info['play_sum_flag'] = "<span style='color: green'>↑</span>";
                }elseif ($info['play_sum'] == $infos['play_sum']){
                    $info['play_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                }else{
                    $info['play_sum_flag'] = "<span style='color: red'>↓</span>";
                }

                if ($info['plays_sum'] > $infos['plays_sum']){
                    $info['plays_sum_flag'] = "<span style='color: green'>↑</span>";
                }elseif ($info['plays_sum'] == $infos['plays_sum']){
                    $info['plays_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                }else{
                    $info['plays_sum_flag'] = "<span style='color: red'>↓</span>";
                }

                if ($info['course_sum'] > $infos['course_sum']){
                    $info['course_sum_flag'] = "<span style='color: green'>↑</span>";
                }elseif ($info['course_sum'] == $infos['course_sum']){
                    $info['course_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                }else{
                    $info['course_sum_flag'] = "<span style='color: red'>↓</span>";
                }

                if ($info['evaluate_sum'] > $infos['evaluate_sum']){
                    $info['evaluate_sum_flag'] = "<span style='color: green'>↑</span>";
                }elseif ($info['evaluate_sum'] == $infos['evaluate_sum']){
                    $info['evaluate_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                }else{
                    $info['evaluate_sum_flag'] = "<span style='color: red'>↓</span>";
                }
            }else{
                // 没有数据
                $info['sub_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                $info['reg_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                $info['reg_stu_flag'] = "<span style='color: #F9CB79'>-</span>";
                $info['reg_tea_flag'] = "<span style='color: #F9CB79'>-</span>";
                $info['sign_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                $info['journal_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                $info['homework_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                $info['play_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                $info['plays_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                $info['course_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
                $info['evaluate_sum_flag'] = "<span style='color: #F9CB79'>-</span>";
            }
            return $this->success('成功','',$info);
        }else{
            return ['code' => 0,'msg' => '抱歉，暂无当天数据统计！'];
        }
    }
    public function rank(){
        $userId = session('userId');
        $role = WechatUser::where('openid',$userId)->value('role');
        if ($role == 0 && $userId != "oJuxj0_Ju_RIb2HbwYxXwAdYLch0"){
            // 学生 或者 非 指定人员 暂无权限
            $this->redirect('Journal/null');
        }
        $time = strtotime(input('time')); // 格式 2018-08-09
        $type = input('type');
        switch ($type){
            case 1:  // 签到
                $info = Db::name('paper_sign')->where(['create_time' => $time])->order('percent','desc')->field('school_id,num,sum,percent')->limit(15)->select();
                break;
            case 2: //总结
                $info = Db::name('paper_journal')->where(['create_time' => $time])->order('percent','desc')->field('school_id,num,sum,percent')->limit(15)->select();
                break;
            case 3:  // 作业
                $info = Db::name('paper_homework')->where(['create_time' => $time])->order('percent','desc')->field('school_id,num,sum,percent')->limit(15)->select();
                break;
            default:
                $info = [];
        }
        $this->assign('sign',$info);
        $this->assign('type',$type);
        $this->assign('time',$time);
        return $this->fetch();
    }
    public function more(){
        $time = input('time'); // 格式 2018-08-09
        $type = input('type');
        $len = input('length');
        switch ($type){
            case 1:  // 签到
                $info = Db::name('paper_sign')->where(['create_time' => $time])->order('percent','desc')->field('school_id,num,sum,percent')->limit($len,15)->select();
                break;
            case 2: //总结
                $info = Db::name('paper_journal')->where(['create_time' => $time])->order('percent','desc')->field('school_id,num,sum,percent')->limit($len,15)->select();
                break;
            case 3:  // 作业
                $info = Db::name('paper_homework')->where(['create_time' => $time])->order('percent','desc')->field('school_id,num,sum,percent')->limit($len,15)->select();
                break;
            default:
                $info = [];
        }
        if ($info){
            foreach ($info as $key => $value){
                $info[$key]['school'] = get_school($value['school_id']);
            }
            return ['code' => 1,'msg' => '成功','data' => $info,'len' => $len];
        }else{
            return ['code' => 0,'msg' => '失败','data' => $info,'len' => $len];
        }
    }
    public function ranks(){
        $userId = session('userId');
        $role = WechatUser::where('openid',$userId)->value('role');
        if ($role == 0 && $userId != "oJuxj0_Ju_RIb2HbwYxXwAdYLch0"){
            // 学生 或者 非 指定人员 暂无权限
            $this->redirect('Journal/null');
        }
        $time = strtotime(input('time')); // 格式 2018-08-09
        $type = input('type');
        switch ($type){
            case 1:  // 总结批阅
                $info = Db::name('paper_play')->where(['create_time' => $time,'type' => 0])->order('sum','desc')->field('openid,sum')->limit(15)->select();
                break;
            case 2:
                $info = Db::name('paper_play')->where(['create_time' => $time,'type' => 1])->order('sum','desc')->field('openid,sum')->limit(15)->select();
                break;
            default:
                $info = [];
        }
        foreach ($info as $key => $value){
            $User = WechatUser::where('openid',$value['openid'])->field('name,headimgurl')->find();
            $info[$key]['name'] = $User['name'];
            $info[$key]['header'] = $User['headimgurl'];
        }
        $this->assign('sign',$info);
        $this->assign('type',$type);
        $this->assign('time',$time);
        return $this->fetch();
    }
    public function mores(){
        $time = input('time'); // 格式 2018-08-09
        $type = input('type');
        $len = input('length');
        switch ($type){
            case 1:  // 总结批阅
                $info = Db::name('paper_play')->where(['create_time' => $time,'type' => 0])->order('sum','desc')->field('openid,sum')->limit($len,15)->select();
                break;
            case 2:
                $info = Db::name('paper_play')->where(['create_time' => $time,'type' => 1])->order('sum','desc')->field('openid,sum')->limit($len,15)->select();
                break;
            default:
                $info = [];
        }
        if ($info){
            foreach ($info as $key => $value){
                $User = WechatUser::where('openid',$value['openid'])->field('name,headimgurl')->find();
                $info[$key]['name'] = $User['name'];
                $info[$key]['header'] = $User['headimgurl'];
            }
            return ['code' => 1,'msg' => '成功','data' => $info,'len' => $len];
        }else{
            return ['code' => 0,'msg' => '失败','data' => $info,'len' => $len];
        }
    }
}